home *** CD-ROM | disk | FTP | other *** search
- YACLP (Yet Another Command Line Parser)
-
- This code is freeware, uploaded by the author. It is provided as is. Please
- use freely. Any comments, suggestions etc. to
-
- Roger Hirst
- CIS:100014,2067
-
- Thanks to John W.Small, Power Software for the original idea.
-
- The current version is 1.1 which contains a minor bug fix and adds getFirst,
- getLast, getNext and getPrev to the Name, Option and Error lists. These
- functions allow you more flexibility in the way in which you choose to
- process the command line.
-
- CL is a total overkill for command line processing but it was interesting
- writing it. There are now more functions than can be easily used in one
- application. But there is the flexibility for you to pick the method of
- access which you prefer and discard the other functions.
-
- The following files make up CL
-
- CL.H - The class definition header file
- CL.CPP - The source file for CL
- CLTEST.CPP - The test harness
- CL.DOC - This file
-
- This command line parser class was designed as an exercise in the use of
- Borland's container Array class. You will need access to the Borland
- container class libraries in order to build the test program and to
- use the code in your own programmes.
-
- The public interface to the CmdLn class is designed for simplicity.
-
- Any number of command line names and/or options are supported.
- Options are identified by a - or / character, which may be changed by
- modifying "switches" in CL.CPP.
-
- static char *switches = "/-";
-
- A typical command line might be:
-
- CLTEST -v -c /pA5 \dos\test1*.doc -e.XYZ -gh
-
- Names and options are delimited by whitespace.
-
- Member Functions
-
- CmdLn (char *);
-
- The class constructor, taking a set of possible options as its
- parameter. Any option taking an additional argument is followed
- by a colon.
-
- CmdLn CL ("cCe:E:gGhHl:L:o:O:P:p:vVxX");
-
- c, g, h, v and x (upper and lower case) are simple options.
- e, l, o, and p take an additional argument.
-
- Any options not found in the parameter to the constructor are saved
- as Error Options. Thus invalid command line options are not seen by
- the programme but it is possible to determine the number of errors
- and the option characters which caused them.
-
- There are only option errors, names can never be in error (?).
-
- Names containing the wildcard characters ? and * are expanded.
-
- ~CmdLn ();
-
- The class destructor.
-
- char *version ();
-
- Returns a pointer to a string representing the current version number.
-
- int numNames ();
-
- Returns the number of valid names parsed.
-
- char *getName (int i);
-
- Returns a pointer to the i th name. An invalid value for i returns
- NULL.
-
- char *getFirstName ();
-
- Returns a pointer to the first name in the list or NULL
- if nothing is found.
-
- char *getLastName ();
-
- Returns a pointer to the last name in the list or NULL
- if nothing is found.
-
- char *getNextName ();
-
- Returns a pointer to the next name in the list or NULL
- if nothing is found.
-
- char *getPrevName ();
-
- Returns a pointer to the previous name in the list or NULL
- if nothing is found.
-
- int numOptions ();
-
- Returns the number of valid options parsed.
-
- char getOption (int i,char *&optarg);
-
- Returns the i th option and any additional argument. An invalid value
- for i returns CL_ENDOFLIST for the option character.
-
- char getFirstOption (char *&optarg);
-
- Returns the first option in the list or CL_ENDOFLIST if nothing is
- found.
-
- char getLastOption (char *&optarg);
-
- Returns the last option in the list or CL_ENDOFLIST if nothing is
- found.
-
- char getNextOption (char *&optarg);
-
- Returns the next option in the list or CL_ENDOFLIST if nothing is
- found.
-
- char getPrevOption (char *&optarg);
-
- Returns the previous option in the list or CL_ENDOFLIST if nothing is
- found.
-
- int numErrors ();
-
- Returns the number of invalid options parsed.
-
- char getError (int i);
-
- Returns the i th error option detected. An invalid value
- for i returns CL_ENDOFLIST for the option character.
-
- char getFirstError ();
-
- Returns the first error in the list or CL_ENDOFLIST if nothing is
- found.
-
- char getLastError ();
-
- Returns the last error in the list or CL_ENDOFLIST if nothing is
- found.
-
- char getNextError ();
-
- Returns the next error in the list or CL_ENDOFLIST if nothing is
- found.
-
- char getPrevError ();
-
- Returns the next error in the list or CL_ENDOFLIST if nothing is
- found.
-
- void add (char *);
-
- Adds additional items to those parsed from the command line.
- Permits the same parsing of command line and user entered input.
-
- void printContentsOn (Rostream os);
-
- Prints the contents of the Name, Option and Error arrays.
- Intended for diagnostic purposes.
-
- void clearNames ();
-
- Clears all name entries.
-
- void clearOptions ();
-
- Clears all option entries.
-
- void clearErrors ();
-
- Clears all error option entries.
-
- void clearAll ();
-
- Clears name, option and error entries.
-
- void reset ();
-
- Clears all entries and reparses the command line.
-
- CLTEST.CPP
-
- Initialises the command line to respond to the options c,e,g,h,l,o,p,v
- and x. The constructor parses the command line for valid options and
- file names. Any wild card file names are expanded.
-
- CmdLn CL ("cCe:E:gGhHl:L:o:O:P:p:vVxX");
-
- To cope with the situation where the user does not input options and file
- names on the command line the add member function is provided. Options and
- wild card file names are translated by the add function
-
- cout << "Enter additional test fields (Names or Options) - ";
- cin.getline (s,80);
- CL.add (s);
-
- Various tests of the options follow.
-
- Additional Notes
-
- CL.CPP contains a correction to the Borland Array class. After detaching
- an Object from the Array the add member function simply carries on adding to
- the end of the array thus generating a sparse Array. This causes great
- confusion when printContentsOn or some other function is called which
- assumes a dense Array.
-
- Note that calls to any of the getNext... getPrev... functions beyond
- the begining or end of a list does no harm, CL_ENDOFLIST will be
- returned but the index to the array will still be incremented/decremented.
- This can cause confusion if you call getNext... beyond the end of
- the list and then expect getPrev... to return something sensible
- on the first call. It is best to start all sequences of getNext...
- or getPrev... with a call to getFirst... or getLast...
-
- CL_ENDOFLIST (-1) has been added as the "nothing found" return code
- replacing '?' this allows '?' to be used as a command line option.
-
- Normally the question mark would be used to support a help function:
-
- program -?
-
- Care should be taken with the signed/unsigned character compiler
- option. This can cause the CL_ENDOFLIST test to fail. The test code
- was compiled with signed characters. If you need unsigned chars simply
- redefine CL_ENDOFLIST.
-
-
-
- Roger Hirst
- 7th December 1991
-